<?php
namespace app\admin\controller;
use think\Db;
use my\Front;
class Api extends Base
{
  //上传图片
  function upimg($dir='default',$thumb=0){
    if(is_post()){
        //阿里云上传
        if($this->sys['is_upload_aliyun']=='上传阿里云'){
                // $aly = [
                //     'KeyId'      => 'LTAIpzyE4eJPNrMx',  //您的Access Key ID
                //     'KeySecret'  => 'A5mg80W9Fdhc6ONYefIA1B1TylO4O5',  //您的Access Key Secret
                //     'Endpoint'   => 'oss-cn-beijing.aliyuncs.com',  //阿里云oss 外网地址endpoint
                //     'Bucket'     => 'alywanhe2017',  //Bucket名称
                // ];
                $aly = json_decode(htmlspecialchars_decode($this->sys['alyun_oss_config']),true);
                $file = date("ymd")."_".time()."_".rand(10000,99999)."_".$this->userinfo['id'].".jpg";
                $oss=new \OSS\OssClient($aly['KeyId'],$aly['KeySecret'],$aly['Endpoint']);
                $oss->uploadFile($aly['Bucket'],$file,$_FILES['file']['tmp_name']);


                $rs = [
                    'code' => 0,
                    'msg'  => '上传成功!',
                    'data' => ['src'=>"http://{$aly['Bucket']}.{$aly['Endpoint']}/".$file,'title'=>''],
                ];
                exit(json_encode($rs));
        }

        //本地上传
        $thumb = $thumb ? true : false;
        $path = com_up_pic($dir, 'file', $thumb);
        if(Front::$config['r']==1){
          $rs = [
            'code' => 1,
            'msg'  => Front::$config['msg'],
            'data' => ['src'=>'','title'=>''],
          ];
        }else{
          $rs = [
            'code' => 0,
            'msg'  => '上传成功!',
            'data' => ['src'=>"/".$path,'title'=>''],
          ];
        }
        exit(json_encode($rs));
    }
  }
  //上传wang编辑器图片
  function upweditorimg($dir='default',$thumb=0){
    if(is_post()){
        //阿里云上传
        if($this->sys['is_upload_aliyun']=='上传阿里云'){
                // $aly = [
                //     'KeyId'      => 'LTAIpzyE4eJPNrMx',  //您的Access Key ID
                //     'KeySecret'  => 'A5mg80W9Fdhc6ONYefIA1B1TylO4O5',  //您的Access Key Secret
                //     'Endpoint'   => 'oss-cn-beijing.aliyuncs.com',  //阿里云oss 外网地址endpoint
                //     'Bucket'     => 'alywanhe2017',  //Bucket名称
                // ];
                $aly = json_decode($this->sys['alyun_oss_config'],true);
                $oss=new \OSS\OssClient($aly['KeyId'],$aly['KeySecret'],$aly['Endpoint']);
                $data = [];
                foreach($_FILES as $val){
                    $file = date("ymd")."_".time()."_".rand(10000,99999)."_".$this->userinfo['id'].".jpg";
                    $oss->uploadFile($aly['Bucket'],$file,$val['tmp_name']);
                    $data[] = "http://{$aly['Bucket']}.{$aly['Endpoint']}/".$file;
                }
                $rs = [
                    'errno' => 0,
                    'data' => $data,
                ];
                exit(json_encode($rs));
        }

        //本地上传
        $thumb = $thumb ? true : false;
        $path = com_up_pic($dir, 'file', $thumb);
        if(Front::$config['r']==1){
          $rs = [
            'errno' => 1,
            'data' => [],
          ];
        }else{
          $rs = [
            'errno' => 0,
            'data' => ["/".$path],
          ];
        }
        exit(json_encode($rs));
    }
  }

  # 更新表的排序
  function sorts(){
      $value = (int)input('value');
      $id = input('id');
      $table = input('table');
      if(is_int($value)){
        Db::name($table)->update(['id'=>$id,'sort'=>$value]);
      }else{
        com_error('请填写数字');
      }
      com_check(['suc_msg'=>'更新成功!','suc_js'=>'alert|reload']);
  }
  # 拷贝一条数据
  function copys($table,$id){
    $info = Db::name($table)->find($id);
    if(in_array($table,['goods']) and $info){
        unset($info['id']);
        $info['create_time'] = time();
        $info['update_time'] = time();
        $info['title'] .= ' 拷贝';
        Db::name($table)->insert($info);
    }
    com_check(['suc_msg'=>'拷贝成功!','suc_js'=>'alert|reload']);
  }
}
